const Logger = require("../core/utils/Logger");
const {RaUser} = require("../database/modes");
const {BaseService} = require("../core/base/BaseService");
const {result_text} = require("../core/base/Response");

class UserService extends BaseService {


    /**
     * 用户登录服务
     */
    async userLogin(profileData) {
        if (this.isEmpty(profileData)) {
            return "请把信息填写完整";
        }
        var {username, password} = profileData;
        if (this.isEmpty(username)) return "请填写用户名"
        if (this.isEmpty(password)) return "请填写密码"
        let user = await RaUser.findOne({where: {ra_username: username, ra_password: password}})
        Logger.e("userLogin user info  : " + user)
        if (this.isEmpty(user)) {
            return "登录失败"
        }
        return null;
    }

    /**
     * 用户注册服务
     */
    async userRegister(profileData) {
        if (this.isEmpty(profileData)) {
            return "请把信息填写完整";
        }
        var {username, password} = profileData;
        if (this.isEmpty(username)) return "请填写用户名"
        if (this.isEmpty(password)) return "请填写密码"
        let user = await RaUser.findOne({where: {ra_username: username}})
        if (!this.isEmpty(user)) {
            return "用户名已存在"
        }
        let saveUser = await RaUser.create({ra_username: username, ra_password: password})
        if (this.isEmpty(saveUser)) {
            return "注册失败"
        }
        return result_text.success_text;
    }
}


const userService = new UserService();
module.exports = userService;
